package nix.anim.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import nix.anim.cms.service.CmsCateService;
import nix.anim.common.pojo.NixResult;
import nix.anim.common.pojo.TreeNode;
import nix.anim.mapper.CmsCategoryMapper;
import nix.anim.mapper.CmsSiteMapper;
import nix.anim.pojo.CmsCategory;
import nix.anim.pojo.CmsCategoryExample;
import nix.anim.pojo.CmsCategoryExample.Criteria;
import nix.anim.pojo.CmsSite;

/**
 * 栏目管理Service
 */
@Service
public class CmsCateServiceImpl implements CmsCateService {

	@Autowired
	private CmsCategoryMapper cmsCateMapper;
	
	@Autowired
	private CmsSiteMapper cmsSiteMapper;
	
	@Value("${ROOT_CMS_CATE_ID}")
	private Integer ROOT_CMS_CATE_ID;

	@Override
	public NixResult getCmsCateTree(Integer parentId, Integer siteId) {
		// 调用递归方法，获得List<TreeNode>
		List<TreeNode> list = getCmsCateNodesByParentId(parentId, siteId);
		return NixResult.ok(list);
	}
	
	private List<TreeNode> getCmsCateNodesByParentId(Integer parentId, Integer siteId) {
		List<TreeNode> list = new ArrayList<>();
		// 查询条件：parentId
		CmsCategoryExample cmsCateExample = new CmsCategoryExample();
		cmsCateExample.setOrderByClause("sort asc");
		Criteria criteria = cmsCateExample.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		criteria.andSiteIdEqualTo(siteId);
		criteria.andStatusEqualTo(1);
		List<CmsCategory> cmsCateList = new ArrayList<>();
		cmsCateList = cmsCateMapper.selectByExample(cmsCateExample);	
		// 判断，遍历，组装TreeNode
		if (cmsCateList != null && cmsCateList.size() >= 0) {
			for (CmsCategory cate : cmsCateList) {
				TreeNode node = new TreeNode();
				node.setId(cate.getId());
				node.setName(cate.getName());
				node.setData(cate);
				// 判断：是父节点，则递归子节点
				if (cate.getIsParent()) {
					node.setChildren(getCmsCateNodesByParentId(cate.getId(), siteId));
				}
				list.add(node);
			}
		}
		return list;
	}

	@Override
	public NixResult getCmsCateById(Integer id) {
		if (id != null) {
			CmsCategory cmsCategory = cmsCateMapper.selectByPrimaryKey(id);
			if (cmsCategory != null) {
				return NixResult.ok(cmsCategory);
			} else {
				return NixResult.build(400, "id不存在！");
			}
		} else {
			return NixResult.build(400, "id无效！");
		}
	}

	private List<CmsCategory> listWithTreeOrder;
	
	@Override
	public NixResult getListWithTreeOrder(Integer siteId) {
		// 查询条件
		CmsCategoryExample cmsCateExample = new CmsCategoryExample();
		cmsCateExample.setOrderByClause("sort asc");
		Criteria criteria = cmsCateExample.createCriteria();
		criteria.andSiteIdEqualTo(siteId);
		criteria.andStatusEqualTo(1);
		List<CmsCategory> cmsCateList = cmsCateMapper.selectByExample(cmsCateExample);
		listWithTreeOrder = new ArrayList<>();
		for (CmsCategory cate : cmsCateList) {
			if (cate.getParentId() == ROOT_CMS_CATE_ID) {
				if (!listWithTreeOrder.contains(cate)) {
					listWithTreeOrder.add(cate);
					addChildrenToListWithTreeOrder(cmsCateList, cate.getId());
				}
			}
		}
		return NixResult.ok(this.listWithTreeOrder);
	}
	
	private void addChildrenToListWithTreeOrder(List<CmsCategory> list, Integer parentId) {
		for (CmsCategory cate : list) {
			if (cate.getParentId() == parentId) {
				if (!listWithTreeOrder.contains(cate)) {
					listWithTreeOrder.add(cate);
					addChildrenToListWithTreeOrder(list, cate.getId());
				}
			}
		}
	}

	@Override
	public NixResult addCmsCate(CmsCategory cmsCate) {
		// 数据验证
		// 非空验证
		if (StringUtils.isBlank(cmsCate.getName())) {
			return NixResult.build(400, "栏目名称不能为空！");
		}
		if (cmsCate.getSort() == null) {
			return NixResult.build(400, "排序不能为空！");
		}
		// 父栏目id验证
		CmsCategory parent = cmsCateMapper.selectByPrimaryKey(cmsCate.getParentId());
		if (cmsCate.getParentId() != ROOT_CMS_CATE_ID && parent == null) {
			return NixResult.build(400, "父栏目id不存在！");
		}
		// 站点id验证
		CmsSite cmsSite = cmsSiteMapper.selectByPrimaryKey(cmsCate.getSiteId());
		if (cmsSite == null) {
			return NixResult.build(400, "站点id无效！");
		}
		// 补全属性
		cmsCate.setIsParent(false);
		if (cmsCate.getParentId() == ROOT_CMS_CATE_ID) {
			cmsCate.setLevel(1);
		} else {
			cmsCate.setLevel(parent.getLevel() + 1);
		}
		cmsCate.setCreated(new Date());
		cmsCate.setUpdated(new Date());
		// 添加
		cmsCateMapper.insert(cmsCate);
		// 判断父节点状态
		if (cmsCate.getParentId() != ROOT_CMS_CATE_ID && parent != null && !parent.getIsParent()) {
			parent.setIsParent(true);
			cmsCateMapper.updateByPrimaryKeySelective(parent);
		}
		// 返回结果
		return NixResult.ok();
	}

	@Override
	public NixResult updateCmsCate(CmsCategory cmsCate) {
		// 数据验证
		// id验证
		NixResult result = getCmsCateById(cmsCate.getId());
		if (result.getStatus() != 200) {
			return result;
		}
		// 非空验证
		if (StringUtils.isBlank(cmsCate.getName())) {
			return NixResult.build(400, "栏目名称不能为空！");
		}
		if (cmsCate.getSort() == null) {
			return NixResult.build(400, "排序不能为空！");
		}
		// 父栏目id验证
		CmsCategory parent = cmsCateMapper.selectByPrimaryKey(cmsCate.getParentId());
		if (cmsCate.getParentId() != ROOT_CMS_CATE_ID && parent == null) {
			return NixResult.build(400, "父栏目id不存在！");
		}
		// 站点id验证
		CmsSite cmsSite = cmsSiteMapper.selectByPrimaryKey(cmsCate.getSiteId());
		if (cmsSite == null) {
			return NixResult.build(400, "站点id无效！");
		}
		// 补全属性
		if (cmsCate.getParentId() == ROOT_CMS_CATE_ID) {
			cmsCate.setLevel(1);
		} else {
			cmsCate.setLevel(parent.getLevel() + 1);
		}
		cmsCate.setUpdated(new Date());
		// 添加
		cmsCateMapper.updateByPrimaryKeySelective(cmsCate);
		// 判断父节点状态
		if (cmsCate.getParentId() != ROOT_CMS_CATE_ID && parent != null && !parent.getIsParent()) {
			parent.setIsParent(true);
			cmsCateMapper.updateByPrimaryKeySelective(parent);
		}
		// 返回结果
		return NixResult.ok();
	}

}
